package com.cuit.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cuit.mapper.OrgUserMapper;
import com.cuit.mapper.UsersMapper;
import com.cuit.params.OrgAddUserParams;
import com.cuit.params.OrgUserParams;
import com.cuit.pojo.Users;
import com.cuit.service.OrgUserService;
import com.cuit.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author SYE
 * @date 2023/7/29 9:56
 * @desciption:
 */
@Service
public class OrgUserServiceImpl implements OrgUserService {

    @Autowired
    private OrgUserMapper orgUserMapper;
    @Autowired
    private UsersMapper usersMapper;

    /**
     * 获得全部单位用户信息
     * @param orgId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result orgGetAllUser(String orgId,Integer page, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("orgId",orgId);
        map.put("page",(page-1)*pageSize);
        map.put("pageSize",pageSize);
        List<OrgUserParams> allOrgUser = orgUserMapper.getAllOrgUser(map);
        Long total = orgUserMapper.getAllOrgUserCount(orgId);
        if (allOrgUser.isEmpty()){
            return Result.fail(201,"数据为空");
        }
        Map<String,Object> allOrgUserMap = new HashMap<>();
        allOrgUserMap.put("record",allOrgUser);
        allOrgUserMap.put("total",total);
        return Result.success(allOrgUserMap);
    }

    @Override
    @Transactional
    public Result orgAddUser(OrgAddUserParams orgAddUserParams,String orgId) {
        Users addUsers = new Users();
        LambdaQueryWrapper<Users> queryLoginNameWrapper = new LambdaQueryWrapper<>();
        queryLoginNameWrapper.eq(Users::getLoginName,orgAddUserParams.getLoginName());
        Users users = usersMapper.selectOne(queryLoginNameWrapper);
        if (users!=null){
            return Result.fail(201,"账号重复");
        }
        addUsers.setOrgId(orgId);
        addUsers.setRoleId(orgAddUserParams.getRoleId());
        addUsers.setUsersName(orgAddUserParams.getUsersName());
        addUsers.setLoginName(orgAddUserParams.getLoginName());
        addUsers.setPassword(DigestUtils.md5DigestAsHex(orgAddUserParams.getPassword().getBytes()));
        if (usersMapper.insert(addUsers)!=1){
            return Result.fail(201,"添加用户失败");
        }
        return Result.success(1);
    }

    @Override
    @Transactional
    public Result deleteOrgUser(String orgId, String userId) {
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getOrgId,orgId);
        queryWrapper.eq(Users::getUserId,userId);
        if (usersMapper.delete(queryWrapper)!=1){
            return Result.fail(201,"删除用户失败");
        }
        return Result.success(1);
    }
}
